/**
 * Jaxson ajax
 *
 * Copyright Remedy floateractive, licensed under Apache version 2 license:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0.html
 *
 * This notice must remain in all java source files.
 */
package org.jaxson.validation;

import org.jaxson.util.reflection.ReflectionUtil;

/**
 * @author Joe Maisel
 *
 */
@SuppressWarnings("serial")
public class RangeValidator extends AbstractValidator{

	protected float min = -1, max = -1;
	boolean tooBig = false; 
	boolean tooSmall = false;
	
	/**
	 * @see org.jaxson.validation.FieldValidator#isValid(java.lang.Object)
	 */
	@Override
	public boolean isValid(Object obj) {
		
		Object value = ReflectionUtil.getProperty(obj, property);
		
		if( value != null ){
			
			if( value instanceof Number ){
				Float f = (Float)value;
				tooSmall = f < min;
				tooBig = f > max;
				
				if( f == null || tooBig || tooSmall ){
					return false;
				}
			}
			
			if( value instanceof String ){
				String s = (String) value;
				tooSmall = s.length() < min;
				tooBig = s.length() > max;
				
				if( s == null || tooBig || tooSmall ){
					return false;
				}
			}
			
		}
		else
			return false;
		
		return true;
	}

	public float getMin() {
		return min;
	}

	public void setMin(float min) {
		this.min = min;
	}

	public float getMax() {
		return max;
	}

	public void setMax(float max) {
		this.max = max;
	}
}
